package com.itfreer.gather.main;

import android.os.Bundle;
import android.view.View;
import android.widget.TableLayout;
import android.widget.TextView;

import com.esri.arcgisruntime.concurrent.ListenableFuture;
import com.esri.arcgisruntime.data.Feature;
import com.esri.arcgisruntime.data.FeatureTable;
import com.esri.arcgisruntime.data.Field;
import com.esri.arcgisruntime.layers.FeatureLayer;
import com.itfreer.arcgis.layer.LayerLoader;
import com.itfreer.arcgis.layer.LayerSelect;
import com.itfreer.arcgis.utils.DbValueConvertUtils;
import com.itfreer.gather.R;
import com.itfreer.gis.datasyn.entity.ProjectEntity;
import com.itfreer.gis.datasyn.sub.EditLogService;
import com.itfreer.ui.activity.FormActivity;
import com.itfreer.ui.base.api.OnValueChange;
import com.itfreer.ui.base.domain.DomainRadioView;
import com.itfreer.utils.common.ToastUtils;

import java.util.Map;

/**
 * 定义属性编辑窗口
 */
public class AttributeEditActivity extends FormActivity {

    // 元数据名称
    private String metadata = "zyjc_zy_jg2018";
    private TextView main_attribute_edit_title;

    private TextView main_attribute_edit_wfaj1;
    private TableLayout main_attribute_edit_wfaj2;

    private TextView main_attribute_edit_zl1;
    private TableLayout main_attribute_edit_zl2;

    private TextView main_attribute_edit_fy1;
    private TableLayout main_attribute_edit_fy2;

    private TextView main_attribute_edit_cf1;
    private TableLayout main_attribute_edit_cf2;

    private TextView main_attribute_edit_zz1;
    private TableLayout main_attribute_edit_zz2;

    private TextView main_attribute_edit_gh1;
    private TableLayout main_attribute_edit_gh2;

    private TextView main_attribute_edit_hz1;
    private TableLayout main_attribute_edit_hz2;

    private TextView main_attribute_edit_bch1;
    private TableLayout main_attribute_edit_bch2;

    private TextView main_attribute_edit_dzw1;
    private TableLayout main_attribute_edit_dzw2;

    private DomainRadioView main_attribute_edit_bhyy;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_attribute_edit_activity);

        main_attribute_edit_title = (TextView) findViewById(R.id.main_attribute_edit_title);

        main_attribute_edit_wfaj1 = (TextView) findViewById(R.id.main_attribute_edit_wfaj1);
        main_attribute_edit_zl1 = (TextView) findViewById(R.id.main_attribute_edit_zl1);
        main_attribute_edit_fy1 = (TextView) findViewById(R.id.main_attribute_edit_fy1);
        main_attribute_edit_cf1 = (TextView) findViewById(R.id.main_attribute_edit_cf1);
        main_attribute_edit_zz1 = (TextView) findViewById(R.id.main_attribute_edit_zz1);
        main_attribute_edit_gh1 = (TextView) findViewById(R.id.main_attribute_edit_gh1);
        main_attribute_edit_hz1 = (TextView) findViewById(R.id.main_attribute_edit_hz1);
        main_attribute_edit_bch1 = (TextView) findViewById(R.id.main_attribute_edit_bch1);
        main_attribute_edit_dzw1 = (TextView) findViewById(R.id.main_attribute_edit_dzw1);

        main_attribute_edit_wfaj2 = (TableLayout) findViewById(R.id.main_attribute_edit_wfaj2);
        main_attribute_edit_zl2 = (TableLayout) findViewById(R.id.main_attribute_edit_zl2);
        main_attribute_edit_fy2 = (TableLayout) findViewById(R.id.main_attribute_edit_fy2);
        main_attribute_edit_cf2 = (TableLayout) findViewById(R.id.main_attribute_edit_cf2);
        main_attribute_edit_zz2 = (TableLayout) findViewById(R.id.main_attribute_edit_zz2);
        main_attribute_edit_gh2 = (TableLayout) findViewById(R.id.main_attribute_edit_gh2);
        main_attribute_edit_hz2 = (TableLayout) findViewById(R.id.main_attribute_edit_hz2);
        main_attribute_edit_bch2 = (TableLayout) findViewById(R.id.main_attribute_edit_bch2);
        main_attribute_edit_dzw2 = (TableLayout) findViewById(R.id.main_attribute_edit_dzw2);

        main_attribute_edit_bhyy = (DomainRadioView) findViewById(R.id.main_attribute_edit_bhyy);
        main_attribute_edit_bhyy.setOnValueChange(new OnValueChange() {
            @Override
            public void onChange(String value) {
                setShow(value);
            }
        });

        initBindDomain(metadata);

        init(LayerSelect.instance.getSelectedFeatures().get(0), LayerSelect.instance.getSelectedFeatureLayer(), metadata);
    }

    public void back(View v) {
        finish();
    }

    public void save(View v) {
        Map<String, Object> data = this.getDataBind();

        Feature feature = LayerSelect.instance.getSelectedFeatures().get(0);
        FeatureLayer featureLayer = LayerSelect.instance.getSelectedFeatureLayer();
        FeatureTable featureTable = LayerSelect.instance.getSelectedFeatureLayer().getFeatureTable();

        if (!featureTable.canUpdate(feature)) {
            ToastUtils.showLong(AttributeEditActivity.this,"当前数据类型无法进行编辑，目前arcgis for android 只能针对shape数据进行编辑！");
            return;
        }

        for(String key : data.keySet()) {
            try {
                Field field = featureTable.getField(key);
                Object cv = DbValueConvertUtils.ConvertValue(field.getFieldType(), data.get(key));
                feature.getAttributes().put(key, cv);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

        final ListenableFuture<Void> updateFeatures = featureTable.updateFeatureAsync(feature);
        // 在操作完成的监听事件中判断操作是否成功
        updateFeatures.addDoneListener(new Runnable() {
            @Override
            public void run() {
                try {
                    updateFeatures.get();
                    if (updateFeatures.isDone()) {
                        ToastUtils.showLong(AttributeEditActivity.this,"保存成功！");

                        ProjectEntity config = LayerLoader.instance.getConfig(featureLayer);
                        Object osguid = feature.getAttributes().get("SGUID");
                        if(osguid!=null){
                            EditLogService.instance.editLog(config, osguid.toString());
                        }
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                    ToastUtils.showLong(AttributeEditActivity.this,"保存失败！");
                }
            }
        });
    }

    /**
     * 初始化
     */
    private void init(final Feature feature, final FeatureLayer featureLayer, final String metadata){
        Map<String, Object> featureAttributes = feature.getAttributes();
        ProjectEntity config = LayerLoader.instance.getConfig(featureLayer);
        if(config!=null && config.getLayerName()!=null && !config.getLayerName().equals("")){
            main_attribute_edit_title.setText(config.getLayerName());
        }else{
            main_attribute_edit_title.setText(featureLayer.getName());
        }

        setDataBind(featureAttributes);

        Object bhyy = featureAttributes.get("BHYY");
        if(bhyy!=null){
            setShow(bhyy.toString());
        }
    }

    /**
     * 设置是否显示
     * @param bhyy
     */
    private void setShow(String bhyy){

        main_attribute_edit_wfaj1.setVisibility(View.GONE);
        main_attribute_edit_wfaj2.setVisibility(View.GONE);

        main_attribute_edit_zl1.setVisibility(View.GONE);
        main_attribute_edit_zl2.setVisibility(View.GONE);

        main_attribute_edit_fy1.setVisibility(View.GONE);
        main_attribute_edit_fy2.setVisibility(View.GONE);

        main_attribute_edit_cf1.setVisibility(View.GONE);
        main_attribute_edit_cf2.setVisibility(View.GONE);

        main_attribute_edit_zz1.setVisibility(View.GONE);
        main_attribute_edit_zz2.setVisibility(View.GONE);

        main_attribute_edit_gh1.setVisibility(View.GONE);
        main_attribute_edit_gh2.setVisibility(View.GONE);

        main_attribute_edit_hz1.setVisibility(View.GONE);
        main_attribute_edit_hz2.setVisibility(View.GONE);

        main_attribute_edit_bch1.setVisibility(View.GONE);
        main_attribute_edit_bch2.setVisibility(View.GONE);

        main_attribute_edit_dzw1.setVisibility(View.GONE);
        main_attribute_edit_dzw2.setVisibility(View.GONE);

        if(bhyy==null || bhyy.equals("")){
            return;
        }

        if(bhyy.equals("1")){
            main_attribute_edit_wfaj1.setVisibility(View.VISIBLE);
            main_attribute_edit_wfaj2.setVisibility(View.VISIBLE);
        } else if(bhyy.equals("2")){
            main_attribute_edit_zl1.setVisibility(View.VISIBLE);
            main_attribute_edit_zl2.setVisibility(View.VISIBLE);
        } else if(bhyy.equals("3")){
            main_attribute_edit_fy1.setVisibility(View.VISIBLE);
            main_attribute_edit_fy2.setVisibility(View.VISIBLE);
        } else if(bhyy.equals("4")){
            main_attribute_edit_cf1.setVisibility(View.VISIBLE);
            main_attribute_edit_cf2.setVisibility(View.VISIBLE);
        } else if(bhyy.equals("5")){
            main_attribute_edit_zz1.setVisibility(View.VISIBLE);
            main_attribute_edit_zz2.setVisibility(View.VISIBLE);
        } else if(bhyy.equals("6")){
            main_attribute_edit_gh1.setVisibility(View.VISIBLE);
            main_attribute_edit_gh2.setVisibility(View.VISIBLE);
        } else if(bhyy.equals("7")){
            main_attribute_edit_hz1.setVisibility(View.VISIBLE);
            main_attribute_edit_hz2.setVisibility(View.VISIBLE);
        } else if(bhyy.equals("8")){
            main_attribute_edit_bch1.setVisibility(View.VISIBLE);
            main_attribute_edit_bch2.setVisibility(View.VISIBLE);
        } else if(bhyy.equals("9")){
            main_attribute_edit_dzw1.setVisibility(View.VISIBLE);
            main_attribute_edit_dzw2.setVisibility(View.VISIBLE);
        }
    }
}
